<template>
	<view class="scan-container">
		<view class="scan-box">
			<view class="title">扫码分配号码</view>
			<button class="scan-btn" @click="scanQrCode">扫一扫</button>
			<view v-if="scanResult" class="result-info">
				<view class="info-item">
					<text class="label">工号：</text>
					<text class="value">{{ scanResult.employeeId || '暂无' }}</text>
				</view>
				<view class="info-item">
					<text class="label">姓名：</text>
					<text class="value">{{ scanResult.userName || '暂无' }}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { assignNumber } from '@/api/info.js'
	import { getUser } from '@/api/system/user'
	
	export default {
		data() {
			return {
				scanResult: null,
				userInfo: null
			}
		},
		onLoad() {
			// 获取当前用户信息
			const userInfo = uni.getStorageSync('userInfo')
			if (userInfo && userInfo.user) {
				this.userInfo = userInfo.user
			}
		},
		methods: {
			// 扫码功能
			scanQrCode() {
				uni.scanCode({
					onlyFromCamera: true,
					success: async (res) => {
						try {
							// 解析二维码数据
							const qrData = JSON.parse(res.result)
							console.log('扫码结果：', qrData)
							
							// 验证时间戳
							const today = new Date().toISOString().split('T')[0]
							if (qrData.timestamp !== today) {
								uni.showToast({
									title: '二维码已过期',
									icon: 'none'
								})
								return
							}
							
							// 获取业务员信息
							const userRes = await getUser(qrData.userId)
							if (userRes.code === 200) {
								this.scanResult = userRes.data
								
								// 显示确认对话框
								uni.showModal({
									title: '分配号码',
									content: `是否要为业务员 ${userRes.data.userName} 分配号码？`,
									success: async (modalRes) => {
										if (modalRes.confirm) {
											// 调用分配号码接口
											try {
												const assignRes = await assignNumber({
													userId: qrData.userId,
													employeeId: userRes.data.employeeId,
													level: '暂无'
												})
												
												if (assignRes.code === 200) {
													uni.showToast({
														title: '分配成功',
														icon: 'success'
													})
													// 清空扫描结果
													this.scanResult = null
												} else {
													uni.showToast({
														title: assignRes.msg || '分配失败',
														icon: 'none'
													})
												}
											} catch (error) {
												console.error('分配号码失败：', error)
												uni.showToast({
													title: '分配失败，请重试',
													icon: 'none'
												})
											}
										}
									}
								})
							} else {
								uni.showToast({
									title: '获取业务员信息失败',
									icon: 'none'
								})
							}
						} catch (error) {
							console.error('处理扫码结果失败：', error)
							uni.showToast({
								title: '无效的二维码',
								icon: 'none'
							})
						}
					},
					fail: () => {
						uni.showToast({
							title: '扫码失败，请重试',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.scan-container {
	min-height: 100vh;
	background-color: #f5f7fa;
	padding: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.scan-box {
	background: #fff;
	border-radius: 20rpx;
	padding: 40rpx;
	width: 80%;
	box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.05);
	text-align: center;
	
	.title {
		font-size: 36rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 40rpx;
	}
	
	.scan-btn {
		width: 80%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #007AFF;
		color: #fff;
		font-size: 32rpx;
		border-radius: 40rpx;
		margin: 0 auto 40rpx;
	}
	
	.result-info {
		margin-top: 40rpx;
		padding: 20rpx;
		background: #f8f9fa;
		border-radius: 12rpx;
		
		.info-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 10rpx 0;
			
			.label {
				color: #666;
				font-size: 28rpx;
			}
			
			.value {
				color: #333;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
	}
}
</style>
